#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "ui_VMidi.h"
#include <QMainWindow>
#include <iostream>

#include "vmidi_interface.h"

using namespace std;
using namespace VMidi;

//namespace VMidi {
//    class VMidiInterface;
//}

class MainWindow: public QMainWindow, private Ui_MainWindow {

    Q_OBJECT

public:
    MainWindow (QWidget* parent = 0) : QMainWindow(parent), active(true) {
        initCore();
        initGUI();
    }

    ~MainWindow () {
        finalizeCore();
    }

public slots:
    void on_tblMatrix_cellClicked(int, int);
    void on_tblMatrix_cellChanged(int, int);

    void on_tfPortName_textChanged (const QString&);

    void on_btnCreateInput_clicked();
    void on_btnCreateOutput_clicked();
    void on_btnDeleteInput_clicked();
    void on_btnDeleteOutput_clicked();

    void on_btnClearAllMatrix_clicked();
    void on_btnClearInputMatrix_clicked();
    void on_btnClearOutputMatrix_clicked();
    void on_btnBroadcastMatrix_clicked();
    void on_btnDiagonalMatrix_clicked();

    void on_btnToggleActivation_clicked();

//    void on_actionNew_triggered();
//    void on_actionOpen_triggered();
//    void on_actionSave_triggered();
//    void on_actionSaveAs_triggered();
//    void on_actionQuit_triggered();

 private:
    void initGUI () {
        setupUi(this);
        // TODO : maybe remove this method if unused.
        connectWidgets();
        updateToolboxState();

        btnCreateInput->setEnabled(false);
        btnCreateOutput->setEnabled(false);

        lblInformation->setText("");

        // Disable resizing of the cells.
        // TODO : temporary until text rotation is implemented.
//        tblMatrix->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
        tblMatrix->verticalHeader()->setResizeMode(QHeaderView::Fixed);

        toggleActivation(true);
    }

    /**
      * Initializes the vmidi core system.
      */
    void initCore ();

    /**
      * Connect 2 ports.
      */
    void coreConnect (int portIdIN, int portIdOUT);

    /**
      * Disconnect 2 ports.
      */
    void coreDisconnect (int portIdIN, int portIdOUT);

    /**
      * Finalizes the vmidi core system.
      */
    void finalizeCore();

    /**
      * TODO : Maybe remove this method if unused.
      */
    void connectWidgets();

    /**
      * Makes a port name according to various parameters (prefix or not, current selection, etc.).
      */
    QString mkPortName (VMidiPortType type);

    /**
      * Given a header index and a port type, retrieve the core id stored in it (Qt::UserRole).
      */
    int getCoreId (int index, VMidiPortType type);

    /**
      * Given a list of selected items in the table, find all the
      * rows and columns selected by those items.
      */
    void findSelectedRowsAndColumns (QSet<int>& rows, QSet<int>& cols, QModelIndexList& items);
    /**
      * Updates the state of the buttons in the toolbox, according to the current state of the matrix.
      */
    void updateToolboxState();

    /**
      * Event filter used to know on which cell the mouse cursor is, to display
      * information about the connections.
      * TODO : not working.
      */
    bool eventFilter(QObject *object, QEvent *event);

    /**
      * Updates the core system and activation info label.
      */
    void toggleActivation (bool state);

 private:
    // Map associating an ID in the GUI with an ID in the vmidi core system.
//    QMap<int,int> idMapIN;
//    QMap<int,int> idMapOUT;

    // Core system.
    VMidiInterface* vmidiCore;

    // Current activation state (default = true).
    bool active;
};

#endif // MAINWINDOW_H
